#include "adcmodule.h"

#include <QDebug>

using namespace Moolink;


//读取AD模块数据
QByteArray ADCmodule::getReadCmd(Device *device,int funCode)
{
    QByteArray ba=device->protocol.toLatin1();
    const char *protocol_char=ba.data();
    QMetaEnum metaEnum=QMetaEnum::fromType<protocolList>();
    protocolList proInt=protocolList(metaEnum.keyToValue(protocol_char));
    QString addrStr;
    addrStr.sprintf("%02X",device->comAddr);
    //QString crcStr;
    //crcStr=sprintf("%02X",(addrStr.at(0).toLatin1()+addrStr.at(1)+'R'+'D')%255);

    QString cmdStr="";

    switch (proInt) {
    case ADAM40170020mA:

    case ADAM40170420mA:

    default:
        cmdStr="#"+addrStr+"\r";
        break;
    }
    //qDebug()<<proInt;
    return cmdStr.toLatin1();//tprotocol;//cmdStr;
}



QString ADCmodule::getDataResultStr(Device *device,QByteArray responseData)
{
    QString recvData = QString::fromLatin1(responseData);
    QByteArray ba=device->protocol.toLatin1();
    const char *protocol_char=ba.data();
    QMetaEnum metaEnum=QMetaEnum::fromType<protocolList>();
    protocolList proInt=protocolList(metaEnum.keyToValue(protocol_char));

    QString pvStr;
    double CH[16];

    QString ResultStr="";

//    QString addrStr;
//    addrStr.sprintf("%02X",device->comAddr);
    if(recvData.size()!=58) {ResultStr="Error:length!=58 error:"+recvData; return ResultStr;}
    if(!recvData.startsWith(">")) {ResultStr="Error:Not startWith >:"+recvData;  return ResultStr;}
    if(!recvData.endsWith("\r")) {ResultStr="Error:Not endWith \\r:"+recvData;  return ResultStr;}



    switch (proInt) {
    case ADAM40170020mA:
        CH[0]=recvData.mid(1,7).toDouble()/20;
        CH[1]=recvData.mid(8,7).toDouble()/20;
        CH[2]=recvData.mid(15,7).toDouble()/20;
        CH[3]=recvData.mid(22,7).toDouble()/20;
        CH[4]=recvData.mid(29,7).toDouble()/20;
        CH[5]=recvData.mid(36,7).toDouble()/20;
        CH[6]=recvData.mid(43,7).toDouble()/20;
        CH[7]=recvData.mid(50,7).toDouble()/20;
        break;

    case ADAM40170420mA:
        CH[0]=(recvData.mid(1,7).toDouble()-4)/16;
        CH[1]=(recvData.mid(8,7).toDouble()-4)/16;
        CH[2]=(recvData.mid(15,7).toDouble()-4)/16;
        CH[3]=(recvData.mid(22,7).toDouble()-4)/16;
        CH[4]=(recvData.mid(29,7).toDouble()-4)/16;
        CH[5]=(recvData.mid(36,7).toDouble()-4)/16;
        CH[6]=(recvData.mid(43,7).toDouble()-4)/16;
        CH[7]=(recvData.mid(50,7).toDouble()-4)/16;
        break;
    case ADAM40170010V:
        CH[0]=recvData.mid(1,7).toDouble()/10;
        CH[1]=recvData.mid(8,7).toDouble()/10;
        CH[2]=recvData.mid(15,7).toDouble()/10;
        CH[3]=recvData.mid(22,7).toDouble()/10;
        CH[4]=recvData.mid(29,7).toDouble()/10;
        CH[5]=recvData.mid(36,7).toDouble()/10;
        CH[6]=recvData.mid(43,7).toDouble()/10;
        CH[7]=recvData.mid(50,7).toDouble()/10;
        break;

    default:
            ResultStr="Error:Not Process:"+recvData;
            return ResultStr;
        break;
    }


    for(int i=0;i<device->deviceCodes.size();i++)
    {
            if(device->deviceCodes.at(i)->code.startsWith("w01001"))
            {

                if(CH[device->deviceCodes.at(i)->rtdDataCh]>=-0.0006)
                {
                    device->deviceCodes.at(i)->dataFlag=dataFlagNormal;
                    if(CH[device->deviceCodes.at(i)->rtdDataCh]<0) CH[device->deviceCodes.at(i)->rtdDataCh]=0;
                    pvStr=HJ212::formatNumber(device->deviceCodes.at(i)->dataFormat,14*CH[device->deviceCodes.at(i)->rtdDataCh]);
                    device->deviceCodes.at(i)->cpData["Rtd"]=pvStr;
                    device->deviceCodes.at(i)->cpData["RtdFlag"]=dataFlagNormal;
                    device->deviceCodes.at(i)->dataString="Rtd:"+pvStr+" "+device->deviceCodes.at(i)->unit;
                }
                else
                {
                    device->deviceCodes.at(i)->dataFlag=dataFlagTransfinite;
                    device->deviceCodes.at(i)->cpData["Rtd"]="0.00";
                    device->deviceCodes.at(i)->cpData["RtdFlag"]=dataFlagTransfinite;
                    device->deviceCodes.at(i)->dataString="无法解析数据,数值超限,请确认接线正确且连接可靠";
                }

            }
            else
            {
                device->deviceCodes.at(i)->dataString="无法装载数据,请确认所绑定的设备支持本因子代码!";
            }
    }



    return recvData;//tprotocol;//cmdStr;

}
